package com.arges.web.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.common.SysEnum.Bllx;
import com.arges.web.common.SysEnum.Fhcd;
import com.arges.web.common.SysEnum.TNM;
import com.arges.web.common.SysEnum.Zlbw;
import com.arges.web.dao.GenericDao;
import com.arges.web.dao.PthlgyDao;
import com.arges.web.domain.bean.PatBean;
import com.arges.web.domain.pojo.Pat;
import com.arges.web.domain.pojo.Pthlgy;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.PatManager;
import com.arges.web.service.PthlgyManager;

/**
 * 患者基本信息业务层
 *
 * @author zxj
 *
 */
@Service
public class PthlgyManagerImpl extends GenericManageImpl<Pthlgy, PatBean> implements PthlgyManager
{
	private Logger LOGGER = LoggerFactory.getLogger(PthlgyManagerImpl.class);

	@Autowired
	private PthlgyDao pthlgyDao;

	@Autowired
	PatManager patManager;

	@Override
	public GenericDao<Pthlgy, Long> getGenericDao()
	{
		return pthlgyDao;
	}

	@Override
	public PatBean getPthlgyByMrn(String mrn)
	{
		PatBean patBean = patManager.getPatByMrn(mrn);
		Pthlgy pthlgy = pthlgyDao.getPthlgyByMrn(mrn);
		Map<String, String> zlbwMap = new HashMap<String, String>();
		for (Zlbw zlbw : Zlbw.values())
		{
			zlbwMap.put(String.valueOf(zlbw.getId()), zlbw.name());
		}

		Map<String, String> fhcdMap = new HashMap<String, String>();
		for (Fhcd fhcd : Fhcd.values())
		{
			fhcdMap.put(String.valueOf(fhcd.getId()), fhcd.name());
		}

		Map<String, String> tnmMap = new HashMap<String, String>();
		for (TNM tnm : TNM.values())
		{
			tnmMap.put(String.valueOf(tnm.getId()), tnm.name());
		}

		Map<String, String> bllxMap = new HashMap<String, String>();
		for (Bllx bllx : Bllx.values())
		{
			bllxMap.put(String.valueOf(bllx.getId()), bllx.name());
		}

		if (StringUtils.isNotBlank(pthlgy.getTumor_site()))
		{
			patBean.setTumor_site(zlbwMap.get(pthlgy.getTumor_site()));
		}
		if (StringUtils.isNotBlank(pthlgy.getDffrnttn()))
		{
			patBean.setDffrnttn(fhcdMap.get(pthlgy.getDffrnttn()));
		}
		if (StringUtils.isNotBlank(pthlgy.getTnm()))
		{
			patBean.setTnm(tnmMap.get(pthlgy.getTnm()));
		}
		if (StringUtils.isNotBlank(pthlgy.getPathology()))
		{
			patBean.setPathology(bllxMap.get(pthlgy.getPathology()));
		}
		if (StringUtils.isNotBlank(pthlgy.getEgfr()))
		{
			patBean.setEgfr(pthlgy.getEgfr());
		}
		if (StringUtils.isNotBlank(pthlgy.getSmoke()))
		{
			patBean.setSmoke(pthlgy.getSmoke());
		}
		if (StringUtils.isNotBlank(pthlgy.getMedical_history()))
		{
			patBean.setMedical_history(pthlgy.getMedical_history());
		}
		return patBean;
	}

	@Override
	public PatBean findPthlgyById(long parseLong)
	{

		return null;
	}

	@Override
	public PageInfo<PatBean> getPagePthlgy(PageInfo<Pthlgy> pojopInfo, PatBean patBean)
	{
		PageInfo<PatBean> patBeanInfo = null;
		Criterion condiition = null;
		String zlbwString = patBean.getTumor_site();
		String fhcdString = patBean.getDffrnttn();
		String tnmString = patBean.getTnm();
		String bllxString = patBean.getPathology();
		String egfrString = patBean.getEgfr();
		String mrn = patBean.getMrn();
		String smoke = patBean.getSmoke();

		if (StringUtils.isNotBlank(zlbwString))
		{
			Zlbw zlbw = Zlbw.valueOf(patBean.getTumor_site());
			zlbwString = String.valueOf(zlbw.getId());
			condiition = Restrictions.eq("tumor_site", zlbwString);
		}
		if (StringUtils.isNotBlank(mrn))
		{
			Criterion mrnCriterion = Restrictions.like("mrn", mrn, MatchMode.ANYWHERE);
			if (null == condiition)
			{
				condiition = mrnCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, mrnCriterion);
			}
		}
		if (StringUtils.isNotBlank(fhcdString))
		{
			Criterion fhcdCriterion = null;
			Fhcd fhcd = Fhcd.valueOf(fhcdString);
			fhcdString = String.valueOf(fhcd.getId());
			fhcdCriterion = Restrictions.eq("dffrnttn", fhcdString);
			if (null == condiition)
			{
				condiition = fhcdCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, fhcdCriterion);
			}
		}
		if (StringUtils.isNotBlank(tnmString))
		{
			Criterion tnmCriterion = null;
			TNM tnm = TNM.valueOf(tnmString);
			tnmString = String.valueOf(tnm.getId());
			tnmCriterion = Restrictions.eq("tnm", tnmString);
			if (null == condiition)
			{
				condiition = tnmCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, tnmCriterion);
			}
		}
		if (StringUtils.isNotBlank(bllxString))
		{
			Criterion bllxCriterion = null;
			Bllx bllx = Bllx.valueOf(bllxString);
			bllxString = String.valueOf(bllx.getId());
			bllxCriterion = Restrictions.eq("pathology", bllxString);
			if (null == condiition)
			{
				condiition = bllxCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, bllxCriterion);
			}
		}
		if (StringUtils.isNotBlank(egfrString))
		{
			Criterion egfrCriterion = null;
			egfrCriterion = Restrictions.eq("egfr", egfrString);
			if (null == condiition)
			{
				condiition = egfrCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, egfrCriterion);
			}
		}
		if (StringUtils.isNotBlank(smoke))
		{
			Criterion smokeCriterion = null;
			smokeCriterion = Restrictions.eq("smoke", smoke);
			if (null == condiition)
			{
				condiition = smokeCriterion;
			}
			else
			{
				condiition = Restrictions.and(condiition, smokeCriterion);
			}
		}
		PageInfo<Pthlgy> pojoPageList = pthlgyDao.findAll(pojopInfo, Pthlgy.class, condiition);

		Map<String, String> zlbwMap = new HashMap<String, String>();
		for (Zlbw zlbw : Zlbw.values())
		{
			zlbwMap.put(String.valueOf(zlbw.getId()), zlbw.name());
		}

		Map<String, String> fhcdMap = new HashMap<String, String>();
		for (Fhcd fhcd : Fhcd.values())
		{
			fhcdMap.put(String.valueOf(fhcd.getId()), fhcd.name());
		}

		Map<String, String> tnmMap = new HashMap<String, String>();
		for (TNM tnm : TNM.values())
		{
			tnmMap.put(String.valueOf(tnm.getId()), tnm.name());
		}

		Map<String, String> bllxMap = new HashMap<String, String>();
		for (Bllx bllx : Bllx.values())
		{
			bllxMap.put(String.valueOf(bllx.getId()), bllx.name());
		}
		List<Pat> list = patManager.findAll();

		patBeanInfo = WebUtils.pojoToBeanPage(pojoPageList, PatBean.class);
		for (PatBean patbean1 : patBeanInfo.getResult())
		{
			for (Pat pat : list)
			{
				if (pat.getMrn().equals(patbean1.getMrn()))
				{
					patbean1.setName(pat.getName());
				}
			}
			if (null != patbean1.getTumor_site())
			{
				patbean1.setTumor_site(zlbwMap.get(patbean1.getTumor_site()));
			}
			if (null != patbean1.getDffrnttn())
			{
				patbean1.setDffrnttn(fhcdMap.get(patbean1.getDffrnttn()));
			}
			if (null != patbean1.getTnm())
			{
				patbean1.setTnm(tnmMap.get(patbean1.getTnm()));
			}
			if (null != patbean1.getPathology())
			{
				patbean1.setPathology(bllxMap.get(patbean1.getPathology()));
			}
		}
		return patBeanInfo;
	}

	@Override
	public void deleteByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] deleteId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			deleteId[i] = Long.valueOf(split[i]);
		}
		pthlgyDao.deletePthlgyByIds(deleteId);
	}

}
